package mafiagame.roles;

import java.util.List;

import mafiagame.Action;
import mafiagame.Game;
import mafiagame.Mark;
import mafiagame.Mark.MarkScope;
import mafiagame.Mark.MarkType;
import mafiagame.roles.Detective.DetectiveRole;
import mafiagame.Player;
import mafiagame.Role;
import mafiagame.RoleDefinition;

public class SexAddict extends RoleDefinition
{
	public DefinitionType getDefinitionType()
	{
		return DefinitionType.LINKED;
	}

	public String explainRole()
	{
		return "You are the sex addict. This will allow you to sleep with another player. You must choose a partner"
				+ " every night "
				+ "phase. Simply say <b>partner</b> [number] to choose. If you are killed, your partner dies "
				+ "as well. Your partner will be known to everyone.";
	}

	public SexAddict()
	{
		super();
	}

	public void assignRole(Game g, Player p)
	{
		p.roles.add(new SexAddictRole(g, p));
	}

	public String getName()
	{
		return "SEX ADDICT";
	}
	
	public static class SexAddictRole extends Role
	{
		public class Partner extends Role
		{

			public Partner(Game g, Player p)
			{
				super(g, p);
			}

			@Override
			public String getName()
			{
				return "PARTNER";
			}

			@Override
			public String getHelp()
			{
				return "If the person that slept with you dies, so do you...";
			}

			public boolean isPopular()
			{
				return true;
			}

		}

		Player myPartner;
		Role myPartnerRole;

		public class ChoosePartnerAction extends Action
		{
			@Override
			public String getDescription()
			{
				return "This will allow you to sleep with another player. You must choose a partner every night "
						+ "phase. Simply say <b>partner</b> [number] to choose. If you are killed, your partner dies "
						+ "as well. Your partner will be known to everyone.";
			}

			@Override
			public String getCommand()
			{
				return "partner";
			}

			@Override
			public boolean Execute(String message)
			{
				String[] tokens = message.split(" ");
				if (tokens.length != 2)
				{
					myGame.communicator
							.sendPrivateMessage(myPlayer.username,
									"The 'partnering' must be of the format 'partner [number]'");
					return false;
				}
				Player p = myGame.parsePlayer(tokens[1]);
				if (p == null)
				{
					myGame.communicator
							.sendPrivateMessage(myPlayer.username,
									"You must choose a player who is actually playing.");
					return false;
				}
				if (!p.living)
				{
					myGame.communicator.sendPrivateMessage(myPlayer.username,
							"You must choose a player who is alive.");
					return false;
				}

				if (p == myPlayer)
				{
					myGame.communicator
							.sendPrivateMessage(myPlayer.username,
									"You cannot partner yourself. That's masturbation.");
					return false;
				}

				Mark m = new Mark();
				m.type = MarkType.DESTINY_BOND;
				m.duration = 2;
				m.scope = MarkScope.BOTH;
				m.isBlockable = true;
				m.isRedirectable = false;
				m.dependentTarget = myPlayer;
				m.source = myPlayer;
				p.marks.add(m);
				
				Mark n = new Mark();
				n.type = MarkType.GIVE_ROLE;
				n.duration = 2;
				n.scope = MarkScope.BOTH;
				n.isBlockable = true;
				n.isRedirectable = false;
				n.source = myPlayer;
				n.givenRole = new Partner(myGame, p);
				p.marks.add(n);

				myPartner = p;

				myGame.communicator.sendPrivateMessage(myPlayer.username,
						"Sleeping with <b>" + p.username + "</b> tonight!");

				return true;
			}

			@Override
			public boolean isPublic()
			{
				return false;
			}

		}

		public SexAddictRole(Game g, Player p)
		{
			super(g, p);
		}

		public String getName()
		{
			return "SEX ADDICT";
		}

		public String getHelp()
		{
			return "You are the sex addict. This will allow you to sleep with another player. You must choose a partner"
					+ " every night "
					+ "phase. Simply say <b>partner</b> [number] to choose. If you are killed, your partner dies "
					+ "as well. Your partner will be known to everyone.";
		}

		@Override
		public void NightPhase()
		{
			myGame.addAction(myPlayer.username,	new ChoosePartnerAction(), true);
		}

		@Override
		public void EndDayPhase()
		{
			
		}
	}
}
